from sqlalchemy import Column, Integer, String, DateTime, Float, Text, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func
import uuid

Base = declarative_base()

class Video(Base):
    """视频模型"""
    __tablename__ = "videos"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    original_filename = Column(String(255), nullable=False, comment="原始文件名")
    stored_filename = Column(String(255), nullable=False, comment="存储文件名")
    file_path = Column(String(500), nullable=False, comment="文件路径")
    file_size = Column(Integer, nullable=False, comment="文件大小(字节)")
    duration = Column(Float, nullable=True, comment="视频时长(秒)")
    resolution = Column(String(50), nullable=True, comment="分辨率")
    frame_rate = Column(Float, nullable=True, comment="帧率")
    original_language = Column(String(10), nullable=True, comment="原始语言代码")
    target_language = Column(String(10), nullable=True, comment="目标语言代码")
    
    # 处理状态
    status = Column(String(20), default="uploaded", comment="处理状态")
    audio_extracted = Column(Boolean, default=False, comment="是否已提取音频")
    audio_path = Column(String(500), nullable=True, comment="音频文件路径")
    transcription_completed = Column(Boolean, default=False, comment="是否已完成转录")
    translation_completed = Column(Boolean, default=False, comment="是否已完成翻译")
    tts_completed = Column(Boolean, default=False, comment="是否已完成语音合成")
    lip_sync_completed = Column(Boolean, default=False, comment="是否已完成唇形同步")
    final_video_path = Column(String(500), nullable=True, comment="最终视频路径")
    
    # 元数据
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), onupdate=func.now(), comment="更新时间")
    description = Column(Text, nullable=True, comment="视频描述")
    
    def __repr__(self):
        return f"<Video(id={self.id}, filename='{self.original_filename}', status='{self.status}')>"
    
    def to_dict(self):
        """转换为字典格式"""
        return {
            'id': self.id,
            'original_filename': self.original_filename,
            'file_size': self.file_size,
            'duration': self.duration,
            'resolution': self.resolution,
            'frame_rate': self.frame_rate,
            'original_language': self.original_language,
            'target_language': self.target_language,
            'status': self.status,
            'audio_extracted': self.audio_extracted,
            'transcription_completed': self.transcription_completed,
            'translation_completed': self.translation_completed,
            'tts_completed': self.tts_completed,
            'lip_sync_completed': self.lip_sync_completed,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'description': self.description
        } 